﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace BEELab
{
    public partial class EditVariable : System.Web.UI.Page
    {
        /// <summary>
        /// Occures on every page load (first time or Post Back)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //if requested sebject ID not allowed to edit variable value - return to main page
            if (Request.QueryString["ID"] != null && BEELab.Logic.ProgramManagement.VariableManagement.VariableManagement.GetOwnerID(int.Parse(Request.QueryString["ID"].ToString())) != int.Parse(Session["UserID"].ToString()) && (Session["Admin"] == null || (bool)Session["Admin"] == false))
            {
                Response.Redirect("Main.aspx");
            }

            //if it is not the first time that the page is loaded
            if (IsPostBack)
            {
                if (Request.QueryString["InTinyMCE"] != null && Request.QueryString["InTinyMCE"].ToString().Equals("true"))
                {
                    literal1.Text = "<script> top.tinymce.activeEditor.windowManager.close(); </script>";
                }
                else
                {
                    literal1.Text = "<script>parent.$.fancybox.close(); window.parent.location.reload();    </script>";
                }

                
            }
            //if page is loaded for the first time
            if (!IsPostBack && Request.QueryString["ID"] != null)
            {
                //gets all variables names exists in this experiment as a string seperated by '|'
                string varNames = BEELab.Logic.ProgramManagement.VariableManagement.VariableManagement.EnumerateVariables(int.Parse(Request.QueryString["ID"].ToString()), "|");
                
                //if there is variables in this experiment 
                //sets the regular expression form that validates variables names
                if (varNames.Length > 0)
                {
                    RegexValidator2.ValidationExpression = "^(?!" + varNames + ").*$";
                }
                //if there is no variables - regular expression is empty (will except any form)
                else
                {
                    RegexValidator2.ValidationExpression = ".*";
                }
                
                //Creates new variable as a copy of existing variable with ID from query string
                BEELab.Logic.ProgramManagement.VariableManagement.VariableManagement.Variable variable = new BEELab.Logic.ProgramManagement.VariableManagement.VariableManagement.Variable(int.Parse(Request.QueryString["ID"].ToString()),0,null);
                txtName.Text = variable.Name;
                txtDescription.Text = variable.Description;
                txtDefaultValue.Text = variable.Value;
                rblScope.SelectedValue = variable.Scope;
            }
        }

        /// <summary>
        /// Update Variable values, or creates new variable with new values (if variable doesn't exists)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button2_Click(object sender, EventArgs e)
        {
            //id variable exists - update its values
            if (Request.QueryString["ID"] != null)
            {
                BEELab.Logic.ProgramManagement.VariableManagement.VariableManagement.UpdateVariable(int.Parse(Request.QueryString["ID"].ToString()), txtName.Text, txtDescription.Text, txtDefaultValue.Text, rblScope.SelectedValue);
                
            }
            //If doesn't exist - create new one
            else if (Request.QueryString["ProgramID"] != null)
            {
                int id = BEELab.Logic.ProgramManagement.VariableManagement.VariableManagement.CreateVariable(int.Parse(Request.QueryString["ProgramID"].ToString()), txtName.Text, txtDescription.Text, txtDefaultValue.Text, rblScope.SelectedValue);
                if (Request.QueryString["InTinyMCE"] == null || Request.QueryString["InTinyMCE"].ToString().Equals("false"))
                {
                    Response.Redirect(Request.RawUrl + "&ID=" + id);
                }
            }
        }
    }
}